<?php

class AnotacaoDAO{

	private $tableName = 'tb_anotacoes';
	private $conn;
	private $sqlGenerator;
	private $prefixo;


	public function __construct(){
		$this->conn = new CONEXAO();
	}

	public function getTableName(){
		return $this->tableName;
	}

	public function setTableName($tableName){
		$this->tableName=$tableName;
	}

	public function getConn(){
		return $this->conn;
	}

	public function setConn($conn){
		$this->conn=$conn;
	}

	public function getSqlGenerator(){
		return $this->sqlGenerator;
	}

	public function setSqlGenerator($sqlGenerator){
		$this->sqlGenerator=$sqlGenerator;
	}

	public function getPrefixo(){
		return $this->prefixo;
	}

	public function setPrefixo($prefixo){
		$this->prefixo=$prefixo;
	}

	public function mapear($obj){

		$array = $obj->toArray();
		
		$this->conn->Conecta();
		
		if(!isset($array['id']) || $array['id']==0){
			$array['id'] = $this->conn->maxId($this->tableName,$this->prefixo) + 1;
		}
		$this->conn->Desconecta();
		
		return $array;

	}

	public function inserir($obj){

			 if(!$obj instanceof Anotacao){
	 	
	 		throw(new Exception("Dados Inapropriados"));
	 	}

	 	$tableName = $this->tableName;
	 	
			
	 	$comando = DMLGenerator::createInsert($tableName,$this->mapear($obj),$this->prefixo);

	 	$this->conn->Conecta();
	 	$this->conn->Executa($comando);
	 	$this->conn->Desconecta();
	 	
	 	return true;

	}

	public function atualizar($obj){

			if(!$obj instanceof Anotacao){
	 	
	 		throw(new Exception("Dados Inapropriados"));
	 	}
	 	
	 	$tableName = $this->tableName;
	 	
	 	
	 	$comando = DMLGenerator::createUpdate($tableName,$this->mapear($obj),$this->prefixo);
	 	
	 	$this->conn->Conecta();
	 	$this->conn->Executa($comando);
	 	$this->conn->Desconecta();

	 	return true;
	}

	public function apagar($obj){

			if(!$obj instanceof Anotacao){
	 	
	 		throw(new Exception("Dados Inapropriados"));
	 	}
	 	
	 	$tableName = $this->tableName;
	 	
	 	
	 	$comando = DMLGenerator::createDelete($tableName,$obj->getId(),$this->prefixo);
	 	
	 	$this->conn->Conecta();
	 	$this->conn->Executa($comando);
	 	$this->conn->Desconecta();

	}

	public function geraWherePesquisa(){

		//TODO

	}

	public function contem(){

		//TODO

	}

	public function buscar($id){

		$this->sqlGenerator = new SQLGenerator($this->tableName);
	 	$this->sqlGenerator->setWhere(array("id = $id"));

	 	$this->conn->Conecta();
	 	
	 	$result = $this->conn->getResultAsVector($this->sqlGenerator->generateSQL());
	 	
	 	$this->conn->Desconecta();
	 	
	 	if(count($result)==1){
	 	
	 	$obj = Anotacao::construct($result[0]);
	 	
	 	return $obj;
	 	}
	 	else{
	 		throw(new Exception("Nenhum objeto encontrado"));
	 		return null;
	 	}

	}

	public function buscarTodos($obj_pesquisa){

	if($obj_pesquisa instanceof Anotacao){
	 	$this->sqlGenerator = new SQLGenerator($this->tableName);
	 	
	 	$this->sqlGenerator->setOrderBy("data DESC");
	 	
	 	$arrayWhere = array();
	 	$attr = $obj_pesquisa->toArray();
	 	
	 	
	 	foreach ($attr as $key => $value){
	 		
	 		if($value !== 0 && $value !== '0' && $value !== '' && $value !== '0000-00-00 00:00:00' ){
	 			
	 			$arrayWhere[] = "".$key." = '".$value."'";
	 		}
	 	}
	 	
	 	$this->sqlGenerator->setWhere($arrayWhere);
	 	
	 	//echo $this->sql->generateSQL();
	 	
	 	$this->conn->Conecta();
	 	 
	 	$result = $this->conn->getResultAsVector($this->sqlGenerator->generateSQL());
	 	 
	 	$this->conn->Desconecta();
	 	
	 	$objArray = array();
	 	
	 	foreach ($result as $array){
	 		
	 		$objArray[] = Anotacao::construct($array);
	 		
	 	}
	 	
	 	return $objArray;
	 	
	 	}
	 	else{
	 		
	 		throw(new Exception("Dados Inapropriados"));
	 	}

	}

	public function buscarTodosSerach(){

		//TODO

	}

}


?>